﻿<Table TItem="BindItem"
       IsPagination="true" PageItemsSource="@PageItemsSource"
       IsStriped="true" IsBordered="true"
       ShowToolbar="true" ShowDefaultButtons="false" ShowSearch="false" IsMultipleSelect="true"
       ShowExtendButtons="true" ExtendButtonColumnWidth="240"
       OnQueryAsync="@OnQueryAsync">
    <TableToolbarTemplate>
        <TableToolbarButton TItem="BindItem" Color="Color.Primary" Icon="fa fa-fw fa-edit" Text="明细" OnClickCallback="@CustomerButton" />
        <TableToolbarButton TItem="BindItem" Color="Color.Success" Icon="fa fa-fw fa-edit" Text="编辑" OnClickCallback="@CustomerButton" />
        <TableToolbarButton TItem="BindItem" Color="Color.Warning" Icon="fa fa-fw fa-edit" Text="权限" OnClickCallback="@CustomerButton" />
        <TableToolbarButton TItem="BindItem" Color="Color.Danger" Icon="fa fa-fw fa-edit" Text="审批" OnClickCallback="@CustomerButton" />
    </TableToolbarTemplate>
    <TableColumns>
        <TableColumn @bind-Field="@context.DateTime" Sortable="true" Width="180" />
        <TableColumn @bind-Field="@context.Name" Sortable="true" Width="100" />
        <TableColumn @bind-Field="@context.Address" Sortable="true" />
    </TableColumns>
    <RowButtonTemplate>
        <TableCellButton Size="Size.ExtraSmall" Color="Color.Primary" TItem="BindItem" Item="@context" Icon="fa fa-edit" Text="明细" OnClickCallback="@OnRowButtonClick" />
        <TableCellButton Size="Size.ExtraSmall" Color="Color.Success" TItem="BindItem" Item="@context" Icon="fa fa-edit" Text="编辑" OnClickCallback="@OnRowButtonClick" />
        <TableCellButton Size="Size.ExtraSmall" Color="Color.Warning" TItem="BindItem" Item="@context" Icon="fa fa-edit" Text="权限" OnClickCallback="@OnRowButtonClick" />
        <TableCellButton Size="Size.ExtraSmall" Color="Color.Danger" TItem="BindItem" Item="@context" Icon="fa fa-edit" Text="审批" OnClickCallback="@OnRowButtonClick" />
    </RowButtonTemplate>
</Table>

@code {
    private static readonly Random random = new Random();

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    protected static List<BindItem> GenerateItems() => new List<BindItem>(Enumerable.Range(1, 80).Select(i => new BindItem()
    {
        Id = i,
        Name = $"张三 {i:d4}",
        DateTime = DateTime.Now.AddDays(i - 1),
        Address = $"上海市普陀区金沙江路 {random.Next(1000, 2000)} 弄",
        Count = random.Next(1, 100),
        Complete = random.Next(1, 100) > 50
    }));

    /// <summary>
    /// 
    /// </summary>
    protected static IEnumerable<BindItem> Items { get; } = GenerateItems();

    /// <summary>
    /// 
    /// </summary>
    protected IEnumerable<int> PageItemsSource => new int[] { 2, 4, 10, 20 };
    
    /// <summary>
    /// 
    /// </summary>
    /// <param name="options"></param>
    /// <returns></returns>
    protected Task<QueryData<BindItem>> OnQueryAsync(QueryPageOptions options) => BindItemQueryAsync(Items, options);

    /// <summary>
    /// 
    /// </summary>
    /// <param name="items"></param>
    protected Task CustomerButton(IEnumerable<BindItem> items)
    {
        var cate = ToastCategory.Information;
        var title = "自定义按钮处理方法";
        var content = $"通过不同的函数区分按钮处理逻辑，参数 Items 为 Table 组件中选中的行数据集合，当前选择数据 {items.Count()} 条";
        ToastService?.Show(new ToastOption()
        {
            Category = cate,
            Title = title,
            Content = content
        });
        return Task.CompletedTask;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="item"></param>
    protected Task OnRowButtonClick(BindItem item)
    {
        var cate = ToastCategory.Success;
        var title = "行内按钮处理方法";
        var content = "通过不同的函数区分按钮处理逻辑，参数 Item 为当前行数据";
        ToastService?.Show(new ToastOption()
        {
            Category = cate,
            Title = title,
            Content = content
        });
        return Task.CompletedTask;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="items"></param>
    /// <param name="options"></param>
    /// <returns></returns>
    protected Task<QueryData<BindItem>> BindItemQueryAsync(IEnumerable<BindItem> items, QueryPageOptions options)
    {
        //TODO: 此处代码后期精简
        if (!string.IsNullOrEmpty(SearchModel.Name)) items = items.Where(item => item.Name?.Contains(SearchModel.Name, StringComparison.OrdinalIgnoreCase) ?? false);
        if (!string.IsNullOrEmpty(SearchModel.Address)) items = items.Where(item => item.Address?.Contains(SearchModel.Address, StringComparison.OrdinalIgnoreCase) ?? false);
        if (!string.IsNullOrEmpty(options.SearchText)) items = items.Where(item => (item.Name?.Contains(options.SearchText) ?? false)
                || (item.Address?.Contains(options.SearchText) ?? false));

        // 过滤
        var isFiltered = false;
        if (options.Filters.Any())
        {
            items = items.Where(options.Filters.GetFilterFunc<BindItem>());

            // 通知内部已经过滤数据了
            isFiltered = true;
        }

        // 排序
        var isSorted = false;
        if (!string.IsNullOrEmpty(options.SortName))
        {
            // 外部未进行排序，内部自动进行排序处理
            var invoker = SortLambdaCache.GetOrAdd(typeof(BindItem), key => items.GetSortLambda().Compile());
            items = invoker(items, options.SortName, options.SortOrder);

            // 通知内部已经过滤数据了
            isSorted = true;
        }

        // 设置记录总数
        var total = items.Count();

        // 内存分页
        items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();

        return Task.FromResult(new QueryData<BindItem>()
        {
            Items = items,
            TotalCount = total,
            IsSorted = isSorted,
            IsFiltered = isFiltered,
            IsSearch = !string.IsNullOrEmpty(SearchModel.Name) || !string.IsNullOrEmpty(SearchModel.Address)
        });
    }    
}
